package com.lxy.my2048;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity{
	private TableLayout tablelayout;
	private TextView[][] tv;

	
	private TextView tvscore;
	private TextView tvhighscore;
	private Button btn_restart;
	
	private int width;
	private int height;
	
	private int score;
	private int highscore;
	private SharedPreferences sp;
	
	private float offsetX;
	private float offsetY;
	
	private int[][]values;

	private Dialog dialog;
	private Dialog dialogVic;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		DisplayMetrics dm = getResources().getDisplayMetrics();
		width = dm.widthPixels;
		height = dm.heightPixels;
		Log.i("width-->",width+"");
		Log.i("height-->",height+"");
		initView();
		
	}
	//
	private void initView(){
		tablelayout=(TableLayout) findViewById(R.id.layout_table);
		tv=new TextView[4][4];
		tv[0][0]=(TextView) findViewById(R.id.tv_00);
		tv[0][1]=(TextView) findViewById(R.id.tv_01);
		tv[0][2]=(TextView) findViewById(R.id.tv_02);
		tv[0][3]=(TextView) findViewById(R.id.tv_03);
		tv[1][0]=(TextView) findViewById(R.id.tv_10);
		tv[1][1]=(TextView) findViewById(R.id.tv_11);
		tv[1][2]=(TextView) findViewById(R.id.tv_12);
		tv[1][3]=(TextView) findViewById(R.id.tv_13);
		tv[2][0]=(TextView) findViewById(R.id.tv_20);
		tv[2][1]=(TextView) findViewById(R.id.tv_21);
		tv[2][2]=(TextView) findViewById(R.id.tv_22);
		tv[2][3]=(TextView) findViewById(R.id.tv_23);
		tv[3][0]=(TextView) findViewById(R.id.tv_30);
		tv[3][1]=(TextView) findViewById(R.id.tv_31);
		tv[3][2]=(TextView) findViewById(R.id.tv_32);
		tv[3][3]=(TextView) findViewById(R.id.tv_33);
		
		btn_restart=(Button) findViewById(R.id.btn_restart);
		btn_restart.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				reStart();
			}
		});
		tvscore=(TextView) findViewById(R.id.tv_score);
		tvhighscore=(TextView) findViewById(R.id.tv_highscore);
		sp=getSharedPreferences("lxy", Context.MODE_PRIVATE);
		highscore=sp.getInt("highscore", 0);
		tvhighscore.setText("最高纪录：\n"+highscore);
		tvscore.setText("得 分：\n"+score);
		//
		int wid=Math.min(width, height);
		LayoutParams params=new LayoutParams(wid, wid);
		tablelayout.setLayoutParams(params);
		//
		int tvwid=(wid-70)/4;
		Log.i("tvwid-->",tvwid+"");
		LayoutParams tvparams=new LayoutParams(tvwid, tvwid);
		tvparams.setMargins(10, 0, 0, 0);
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				tv[i][j].setLayoutParams(tvparams);
			}
		}
		values=new int[4][4];
		random();
		random();
		setNum();
	}
	
	//
	private void reStart(){
		values=new int[4][4];
		score=0;
		random();
		random();
		setNum();
	}
	
	//
	private void setNum(){
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				if(values[i][j]<=0){
					tv[i][j].setText("");
				}else{
					tv[i][j].setText(values[i][j]+"");
				}
				tv[i][j].setBackgroundResource(getColor(values[i][j]));
			}
		}
		
		int over=0;
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				if(values[i][j]==0){
					over+=1;
				}
				if(values[i][j]==2048){
					//victory
					if(score>highscore){
						highscore=score;
						Editor editor=sp.edit();
						editor.putInt("highscore", highscore);
						editor.commit();
						tvhighscore.setText("最高纪录：\n"+highscore);
					}
					dialogVic=new AlertDialog.Builder(this). 
			                setTitle("通关了！！！"). 
			                setMessage("得分："+score+"\n最高纪录："+highscore+"\n重新开始吗？"). 
			                setIcon(R.drawable.ic_launcher). 
			                setPositiveButton("确定", new DialogInterface.OnClickListener() {                     
			                    public void onClick(DialogInterface dialog, int which) { 
			                        reStart();
			                    } 
			                }). 
			                setNegativeButton("取消", new DialogInterface.OnClickListener() {                     
			                    public void onClick(DialogInterface dialog, int which) { 
			                    	dialog.dismiss();
			                    } 
			                }). 
			                create(); 
					dialogVic.show();
				}
			}
		}
		if(over==0){
			//game over
			if(score>highscore){
				highscore=score;
				Editor editor=sp.edit();
				editor.putInt("highscore", highscore);
				editor.commit();
				tvhighscore.setText("最高纪录：\n"+highscore);
			}
			dialog=new AlertDialog.Builder(this). 
	                setTitle("游戏结束"). 
	                setMessage("得分："+score+"\n最高纪录："+highscore+"\n重新开始吗？"). 
	                setIcon(R.drawable.ic_launcher). 
	                setPositiveButton("确定", new DialogInterface.OnClickListener() {                     
	                    public void onClick(DialogInterface dialog, int which) { 
	                        reStart();
	                    } 
	                }). 
	                setNegativeButton("取消", new DialogInterface.OnClickListener() {                     
	                    public void onClick(DialogInterface dialog, int which) { 
	                    	dialog.dismiss();
	                    } 
	                }). 
	                create(); 
			dialog.show(); 
		}
		tvscore.setText("得分\n"+score);
		
	}
	//
	private int getColor(int id){
//		Log.i("color---》",Integer.toString(id));
		int colorID=R.color.c0;
		switch(id){
		case 2: colorID=R.color.c2;break;
		case 4: colorID= R.color.c4;break;
		case 8: colorID=R.color.c8;break;
		case 16: colorID=R.color.c16;break;
		case 32: colorID= R.color.c32;break;
		case 64: colorID=R.color.c64;break;
		case 128: colorID= R.color.c128;break;
		case 256: colorID= R.color.c256;break;
		case 512: colorID= R.color.c512;break;
		case 1024: colorID= R.color.c1024;break;
		case 2048: colorID= R.color.c2048;break;
		}
		return colorID;
	}

	//
	private void slideLeft(){
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				if(values[i][j]==0){
					int ii=j;
					for(int m=j+1;m<4;m++){
						if(values[i][m]!=0){
							values[i][ii]=values[i][m];
							values[i][m]=0;
							ii+=1;
						}
					}
				}
			}
			for(int j=1;j<4;j++){
				if(values[i][j-1]==values[i][j]&&values[i][j]>0){
					values[i][j-1]+=values[i][j];
					score+=values[i][j-1];
					values[i][j]=0;
					for(int m=j+1;m<4;m++){
						values[i][m-1]=values[i][m];
						
					}
				}
			}
		}
		
		
		random();
		setNum();
	}
	private void slideRight(){
		for(int i=0;i<4;i++){
			for(int j=3;j>=0;j--){
				if(values[i][j]==0){
					int ii=j;
					for(int m=j-1;m>=0;m--){
						if(values[i][m]!=0){
							values[i][ii]=values[i][m];
							values[i][m]=0;
							ii-=1;
						}
					}
				}
			}
			for(int j=3;j>0;j--){
				if(values[i][j-1]==values[i][j]&&values[i][j-1]>0){
					values[i][j]+=values[i][j-1];
					score+=values[i][j];
					values[i][j-1]=0;
					for(int m=j-1;m>0;m--){
						values[i][m]=values[i][m-1];
					}
				}
			}
		}
		
		random();
		setNum();
	}
	private void slideUp(){
		for(int j=0;j<4;j++){
			for(int i=0;i<4;i++){
				if(values[i][j]==0){
					int ii=i;
					for(int m=i+1;m<4;m++){
						if(values[m][j]!=0){
							values[ii][j]=values[m][j];
							values[m][j]=0;
							ii+=1;
						}
					}
				}
			}
			for(int i=1;i<4;i++){
				if(values[i-1][j]==values[i][j]&&values[i][j]>0){
					values[i-1][j]+=values[i][j];
					score+=values[i-1][j];
					values[i][j]=0;
					for(int m=i+1;m<4;m++){
						values[m-1][j]=values[m][j];
					}
				}
			}
		}
		
		random();
		setNum();
	}
	private void slideDown(){
		for(int j=0;j<4;j++){
			for(int i=3;i>=0;i--){
				if(values[i][j]==0){
					int ii=i;
					for(int m=i-1;m>=0;m--){
						if(values[m][j]!=0){
							values[ii][j]=values[m][j];
							values[m][j]=0;
							ii-=1;
						}
					}
				}
			}
			for(int i=3;i>0;i--){
				if(values[i-1][j]==values[i][j]&&values[i-1][j]>0){
					values[i][j]+=values[i-1][j];
					score+=values[i][j];
					values[i-1][j]=0;
					for(int m=i-1;m>0;m--){
						values[m][j]=values[m-1][j];
					}
				}
			}
		}
		
		random();
		setNum();
	}
	
	private void random(){
		ArrayList<Integer> list=new ArrayList<Integer>();
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				if(values[i][j]==0){
					list.add(i*4+j);
				}
			}
		}
		if(list.size()<=0) {
			Log.i("","game over!");
			return;
		}
		Random r=new Random(System.currentTimeMillis());
		int i=r.nextInt(list.size());
		int location=list.get(i);
		if(r.nextInt(10)>=1){
			values[location/4][location%4]=2;
		}else{
			values[location/4][location%4]=4;
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		if(event.getAction()==MotionEvent.ACTION_DOWN){
			offsetX=event.getX();
			offsetY=event.getY();
		}
		if(event.getAction()==MotionEvent.ACTION_UP){
			offsetX-=event.getX();
			offsetY-=event.getY();
			if(Math.abs(offsetY)>Math.abs(offsetX)){//上下
				if(offsetY>20){//上
					slideUp();
					Log.i("","up");
				}else if(offsetY<-20){//下
					slideDown();
					Log.i("","down");
				}
			}else{//左右
				if(offsetX>20){//左
					slideLeft();
					Log.i("","left");
				}else if(offsetX<-20){//右
					slideRight();
					Log.i("","right");
				}
			}
			
		}
		
		return false;  
	}

}
